/*
 * Created on 1 Jul 2007
 */
package org.jverify.utility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public final class CompressedStreamHandler {

	private CompressedStreamHandler() {
	}

	public static byte[] compressData(final byte[] data) throws IOException {
		final ByteArrayOutputStream bArOut = new ByteArrayOutputStream();
		final GZIPOutputStream gzip = new GZIPOutputStream(bArOut);
		gzip.write(data, 0, data.length);
		gzip.flush();
		gzip.close();
		return bArOut.toByteArray();
	}

	public static byte[] deCompressData(final byte[] data) throws IOException {
		final ByteArrayInputStream bbIn = new ByteArrayInputStream(data);
		final ByteArrayOutputStream bbOut = new ByteArrayOutputStream();
		final GZIPInputStream gzip = new GZIPInputStream(bbIn);
		int read;
		while ((read = gzip.read()) != -1) {
			bbOut.write(read);
		}
		gzip.close();
		return bbOut.toByteArray();
	}

}
